# Maintainer: Wu, Zhenyu <wuzhenyu@ustc.edu>

_realname=neocmakelsp
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.8.23
pkgrel=1
pkgdesc='CMake LSP implementation based on Tower and Tree-sitter (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
msys2_repository_url='https://github.com/neocmakelsp/neocmakelsp/'
url='https://neocmakelsp.github.io/'
msys2_references=(
  'purl: pkg:cargo/neocmakelsp'
)
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/neocmakelsp/neocmakelsp/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        0001-neocmakelsp-meson-disable-cargo-build.patch)
sha256sums=('3cbc9ce4b49a93a67137af0ebe619c7fa39998376cb5ae75baf53c38211fc6a0'
            'cb5ca660796b33e9834e126c15ea28bc48b2ec7cbc811e2b757462625cf85567')

prepare() {
  cd "${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/0001-neocmakelsp-meson-disable-cargo-build.patch"

  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cd "${_realname}-${pkgver}"

  cargo build --release --frozen

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    meson setup \
      --prefix="${MINGW_PREFIX}" \
      -Ddebug=false \
      "${srcdir}/build-${MSYSTEM}"

  meson compile -C "${srcdir}/build-${MSYSTEM}"
}

check() {
  cd "${_realname}-${pkgver}"

  cargo test --release --frozen
}

package() {
  cd "${_realname}-${pkgver}"

  cargo install \
    --offline \
    --no-track \
    --frozen \
    --path . \
    --root "${pkgdir}${MINGW_PREFIX}"

  meson install -C "${srcdir}/build-${MSYSTEM}" --destdir "${pkgdir}"

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
